package com.lagou.mr.map_join;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

public class MapJoinMapper extends Mapper<LongWritable, Text,Text,NullWritable> {

    //map任务启动时初始化执行一次
    HashMap<String, String> map = new HashMap<>();

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {

        InputStreamReader streamReader = new InputStreamReader(new FileInputStream("E:\\map_join\\cache\\position.txt"), "UTF-8");
        BufferedReader reader = new BufferedReader(streamReader);
        String line ;
        while(StringUtils.isNotEmpty(line = reader.readLine())){
            String[] fields = line.split("\t");
            map.put(fields[0],fields[1]);
        }
        reader.lines();
    }

    Text k = new Text();
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        String[] arr = line.split("\t");
        String positionName = map.get(arr[1]);
        k.set(line+ "\t" + positionName);
        context.write(k, NullWritable.get());
    }
}
